 

package com.zdkj.subway.stm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.constant.SecurityConstants;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.dto.out.SwParamStation;
import com.zdkj.subway.stm.entity.SwStInvoiceBalance;
import com.zdkj.subway.stm.entity.SwStInvoiceUse;
import com.zdkj.subway.stm.fegin.SupportService;
import com.zdkj.subway.stm.service.SwStInvoiceBalanceService;
import com.zdkj.subway.stm.service.SwStInvoiceUseService;
import com.zdkj.subway.stm.util.BeanPropertyUtils;
import com.zdkj.subway.stm.util.ObjectValueUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 车站发票使用台账
 *
 * @author zdkj code generator
 * @date 2020-05-25 10:42:55
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstinvoiceuse" )
@Api(value = "swstinvoiceuse", tags = "车站发票使用台账管理")
public class SwStInvoiceUseController {

    private final  SwStInvoiceUseService swStInvoiceUseService;
    private final SwStInvoiceBalanceService swStInvoiceBalanceService;
    private final SupportService supportService;
   /*
     * 分页关联查询
     * @param page 分页对象
     * @param swStInvoiceUse 车站发票使用台账
     * @return
     *//*
    @ApiOperation(value = "分页关联查询", notes = "分页关联查询")
    @GetMapping("/pageExport" )
    public R getSwStInvoiceUsePageLink(Page page, SwStInvoiceUse swStInvoiceUse) {
        return R.ok(swStInvoiceUseService.pageLink(page, Wrappers.query(swStInvoiceUse)
                .allEq(SelMapUtils.getSwStInvoiceUseMap(swStInvoiceUse),false)
                .eq("a.STATION_CODE",swStInvoiceUse.getStationCode())
        ));
    }*/


    /**
     * 分页查询
     * @param page 分页对象
     * @param swStInvoiceUse 车站发票使用台账
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwStInvoiceUsePage(Page page, SwStInvoiceUse swStInvoiceUse) {
        return R.ok(swStInvoiceUseService.page(page, Wrappers.query(swStInvoiceUse).lambda()
                .eq(SwStInvoiceUse::getStationCode,SecurityUtils.getUser().getStationCode())));
    }


    /**
     * 通过id查询车站发票使用台账
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swStInvoiceUseService.getById(id));
    }

    /**
     * 新增车站发票使用台账
     * @param swStInvoiceUse 车站发票使用台账
     * @return R
     */
    @ApiOperation(value = "新增车站发票使用台账", notes = "新增车站发票使用台账")
    @SysLog("新增车站发票使用台账" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stm_swstinvoiceuse_add')" )
    public R save(@RequestBody SwStInvoiceUse swStInvoiceUse) {
        SwStInvoiceUse defaultObj = ObjectValueUtils.getDefaultObj(SwStInvoiceUse.class);
        BeanUtil.copyProperties(swStInvoiceUse, defaultObj, CopyOptions.create()
                .setIgnoreProperties(BeanPropertyUtils.getNullAndEmptyPropertyNames(swStInvoiceUse)));
        defaultObj.setBalanceNumber(swStInvoiceUse.getSendNumber()-swStInvoiceUse.getUseNumber());
        defaultObj.setConfirmName(SecurityUtils.getUser().getNikeName());
        defaultObj.setConfirmCode(SecurityUtils.getUser().getUserCode());
        return R.ok(swStInvoiceUseService.save(defaultObj));
    }

    /**
     * 修改车站发票使用台账
     * @param swStInvoiceUse 车站发票使用台账
     * @return R
     */
    @ApiOperation(value = "修改车站发票使用台账", notes = "修改车站发票使用台账")
    @SysLog("修改车站发票使用台账" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stm_swstinvoiceuse_edit')" )
    public R updateById(@RequestBody SwStInvoiceUse swStInvoiceUse) {
        return R.ok(swStInvoiceUseService.updateById(swStInvoiceUse));
    }

    /**
     * 根据传入使用数量计算结存
     * @param swStInvoiceUse 车站发票使用台账
     * @return R
     */
    @ApiOperation(value = "根据传入使用数量计算结存", notes = "根据传入使用数量计算结存")
    @SysLog("根据传入使用数量计算结存" )
    @PutMapping("/getBalance")
    public R getBalance(@RequestBody SwStInvoiceUse swStInvoiceUse) {

        if (swStInvoiceUse.getUseNumber()>swStInvoiceUse.getBalanceNumber()){
            R.ok("结存数量小于使用数量");
        }else {
            swStInvoiceUse.setBalanceNumber(swStInvoiceUse.getBalanceNumber()-swStInvoiceUse.getUseNumber());
        }
        swStInvoiceUseService.updateById(swStInvoiceUse);
        /**
         * 如果领用日期在现在之前去修改最近的结存记录
         */
        if (swStInvoiceUse.getSendDate().isBefore(LocalDate.now())){
            SwStInvoiceBalance s = new SwStInvoiceBalance();
            s.setInvoiceType(swStInvoiceUse.getInvoiceType());
            s.setInvoiceMoney(swStInvoiceUse.getInvoiceMoney());
            s.setStationCode(SecurityUtils.getUser().getStationCode());
            //去查找最近的一条结存记录  更改结存  和使用数量
            SwStInvoiceBalance one = swStInvoiceBalanceService
                    .getOne(Wrappers.query(s).lambda().orderByDesc(SwStInvoiceBalance::getStartEndDate).last("LIMIT 1"));
            one.setUseNumber(one.getUseNumber()+swStInvoiceUse.getUseNumber());
            one.setBalanceNumber(one.getBalanceNumber()-swStInvoiceUse.getUseNumber());
            swStInvoiceBalanceService.updateById(one);
        }
        return R.ok("结存更新成功");
    }

    /**
     * 通过id删除车站发票使用台账
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除车站发票使用台账", notes = "通过id删除车站发票使用台账")
    @SysLog("通过id删除车站发票使用台账" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('stm_swstinvoiceuse_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStInvoiceUseService.removeById(id));
    }

    /**
     * 通过ids删除车站发票使用台账
     * @param ids ids
     * @return R
     */
    @ApiOperation(value = "通过id删除车站发票使用台账", notes = "通过id删除车站发票使用台账")
    @SysLog("通过ids删除车站发票使用台账" )
    @DeleteMapping("/delBatch" )
    @PreAuthorize("@pms.hasPermission('stm_swstinvoiceuse_dels')" )
    public R removeByIds(@RequestBody List<Integer> ids) {
        return R.ok(swStInvoiceUseService.removeByIds(ids));
    }

    /**
     * 定时任务  查询每一天的统计初始值，后面那个日期有变化在更新
     * @return
     */
    @ApiOperation(value = "定时任务", notes = "定时任务")
    @GetMapping("/task/count" )
    @Inner
    public R getTimerSave() {
        SwStInvoiceUse swStInvoiceUse = new SwStInvoiceUse();
        /**
         * 统计所有车站前一日的结存
         */
        R<List<SwParamStation>> stationDict = supportService.getStationDict(SecurityConstants.FROM_IN);
        List<SwParamStation> data = stationDict.getData();
        for (SwParamStation datum : data) {
            swStInvoiceUse.setStationCode(datum.getCode());
            swStInvoiceUse.setSendDate(LocalDate.now().plusDays(-1));
            swStInvoiceUseService.timerSave(swStInvoiceUse);
        }
        return R.ok();
    }


}
